package de.wechselfuchs.docgenerator.doc;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import de.wechselfuchs.docgenerator.Environment;
import de.wechselfuchs.docgenerator.tariff.Tariff;

/**
 * This class provides generation methods to generate an offer according to the
 * wechselfuchs layout.
 * 
 * @author Sebastian Richter
 * 
 */

public class Offer extends OfferDocument {

	/* fields */
	private String consultantPhone;

	/**
	 * An object constructed with this constructor will generate the offer doc
	 * including all tariffs of the tariffs parameter. According to the number
	 * of tariffs the layout is adapted.
	 * 
	 * @param tariffs
	 *            The tariffs to be included in the offer doc.
	 */

	public Offer(Tariff[] tariffs) {
		// TODO Constructor
	}

	@Override
	public boolean generateDoc(String path) {
		boolean noError = true;
		Document doc = new Document();
		try {
			// initialization
			PdfWriter wrt = PdfWriter.getInstance(doc, new FileOutputStream(
					Environment.getString("OfferData.File"))); //$NON-NLS-1$
			doc.open();
			doc.setPageSize(PageSize.A4);

			// TODO format everything!

			/*
			 * Header
			 */
			// Header table
			PdfPTable headerTable = new PdfPTable(2);

			// WF logo
			Image img = Image.getInstance(Environment
					.getString("OfferData.Logo")); //$NON-NLS-1$
			PdfPCell logoCell = new PdfPCell(img);
			logoCell.setBorder(PdfPCell.NO_BORDER);
			logoCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

			// Current date
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat();
			sdf.applyLocalizedPattern(Environment
					.getString("OfferData.DateFormat")); //$NON-NLS-1$
			String dateString = sdf.format(cal.getTime());
			PdfPCell dateCell = new PdfPCell(new Phrase(dateString));
			dateCell.setBorder(PdfPCell.NO_BORDER);
			dateCell.setBorder(PdfPCell.ALIGN_RIGHT);

			// Make table
			headerTable.addCell(logoCell);
			headerTable.addCell(dateCell);

			headerTable.setComplete(true);

			/*
			 * Footer
			 */
			// Footer table
			PdfPTable footerTable = new PdfPTable(3);

			// Left block
			Phrase leftBlock = new Phrase();
			leftBlock.add(Environment.getString("OfferData.CompanyName")); //$NON-NLS-1$
			leftBlock.add(Chunk.NEWLINE);
			leftBlock.add(Environment.getString("OfferData.CompanyStreet")); //$NON-NLS-1$
			leftBlock.add(Chunk.NEWLINE);
			leftBlock.add(Environment
					.getString("OfferData.CompanyPostalCodeAndPlace")); //$NON-NLS-1$

			PdfPCell leftCell = new PdfPCell(leftBlock);
			leftCell.setBorder(PdfPCell.NO_BORDER);

			// Middle block
			Phrase middleBlock = new Phrase();
			middleBlock
					.add(Environment.getString("OfferData.TelPrefix") + consultantPhone); //$NON-NLS-1$
			middleBlock.add(Chunk.NEWLINE);
			middleBlock.add(Environment.getString("OfferData.FaxNumber")); //$NON-NLS-1$
			middleBlock.add(Chunk.NEWLINE);
			middleBlock.add(Environment.getString("OfferData.InfoMailAddress")); //$NON-NLS-1$
			middleBlock.add(Chunk.NEWLINE);
			middleBlock.add(Environment.getString("OfferData.URLHomepage")); //$NON-NLS-1$

			PdfPCell middleCell = new PdfPCell(middleBlock);
			middleCell.setBorder(PdfPCell.NO_BORDER);

			// Right block
			Phrase rightBlock = new Phrase();
			rightBlock.add(Environment.getString("OfferData.AccountNumber")); //$NON-NLS-1$
			rightBlock.add(Chunk.NEWLINE);
			rightBlock.add(Environment.getString("OfferData.BankNumber")); //$NON-NLS-1$
			rightBlock.add(Chunk.NEWLINE);
			rightBlock.add(Environment.getString("OfferData.BankName")); //$NON-NLS-1$

			PdfPCell rightCell = new PdfPCell(rightBlock);
			rightCell.setBorder(PdfPCell.NO_BORDER);

			// Make table
			footerTable.addCell(leftCell);
			footerTable.addCell(middleCell);
			footerTable.addCell(rightCell);

			footerTable.setComplete(true);

			// TODO body page 1
			// TODO body page 2
			// TODO merge
			// TODO print
			doc.close();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// TODO set noError to false, if
		return noError;

	}

	@Override
	public boolean generateDoc() {
		return generateDoc(null);
	}

}
